<template>
    <master title="作业发布">
        <el-button @click="drawer = true" type="primary" style="margin-left: 16px;margin: 20px">
            选题信息
        </el-button>
        <el-row style="width: 90%;padding: 50px">



            <el-select v-model="queryParms.type" placeholder="请选择题目类型" @change="getTopicList">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>

            <!-- 左侧当前题目 -->
            <el-col :span="8">
                <el-tree :data="treeData" highlight-current="true" default-expand-all="true"
                    :expand-on-click-node="false" icon-class="none" :props="defaultProps"
                    @node-click="handleNodeClick"></el-tree>
            </el-col>
            <el-col :span="16">

                <el-table ref="multipleTable" :data="topicData" tooltip-effect="dark" style="width: 100%"
                    @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column prop="typeName" label="类型" width="120">
                        <!-- <template slot-scope="scope">{{ scope.row.date }}</template>-->
                    </el-table-column>
                    <el-table-column prop="question" label="题干">
                    </el-table-column>

                </el-table>

            </el-col>
        </el-row>

        <el-drawer title="选题信息" size="70%" style="padding: 10px" :visible.sync="drawer">

            <el-table ref="multipleTable" :data="formInline.multipleSelection" tooltip-effect="dark"
                style="width: 100%">
                <el-table-column type="index" width="55">
                </el-table-column>
                <el-table-column prop="typeName" label="类型" width="120">
                    <!-- <template slot-scope="scope">{{ scope.row.date }}</template>-->
                </el-table-column>
                <el-table-column prop="question" label="题干">
                </el-table-column>

            </el-table>


            <el-form :inline="true" :model="formInline" class="demo-form-inline" style="margin-top: 20px;padding: 20px">

                <el-form-item label="作业类型:">
                    <el-select v-model="formInline.type" placeholder="请选择类型">
                        <el-option v-for="item in typeRows" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="试题名称:">
                    <el-input v-model="formInline.name" placeholder="试题名称"></el-input>
                </el-form-item>
                <el-form-item label="选择题每题分值:">
                    <el-input v-model="formInline.choiceNum" placeholder="选择题每题分值"></el-input>
                </el-form-item>

                <el-form-item label="判断题每题分值：">
                    <el-input v-model="formInline.trueNum" placeholder="判断题每题分值"></el-input>
                </el-form-item>
                <el-form-item label="多选题每题分值：">
                    <el-input v-model="formInline.dxNum" placeholder="多选题每题分值"></el-input>
                </el-form-item>
                <el-form-item label="填空题每题分值：">
                    <el-input v-model="formInline.putNum" placeholder="填空题每题分值"></el-input>
                </el-form-item>
                <el-form-item label="简答题每题分值：">
                    <el-input v-model="formInline.shortNum" placeholder="简答题每题分值"></el-input>
                </el-form-item>

                <el-form-item label="备注信息：">
                    <el-input v-model="formInline.remark" placeholder="备注"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmit" style="width: 100%">发布</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </master>
</template>
<script type="text/javascript">
    (function (G) {
        G.vue({
            "usingComponents": {///引入组件
                "master": "/components/Master/Master"///母版组件
            },
            "enablePullDownRefresh": true
        }, {
            data: {
                formInline: {
                    choiceNum: '',
                    trueNum: '',
                    dxNum: '',
                    putNum: '',
                    type: '0',
                    shortNum: '',
                    name: '',
                    remark: '',
                    multipleSelection: []

                },
                drawer: false,
                topicData: [],

                queryParms: {
                    courseId: '1313921323726012416',
                    type: '0',
                    chapterId: ''
                },
                filterText: '',
                treeData: [],
                options: [{
                    value: '0',
                    label: '请选择题目类型'
                }, {
                    value: '1',
                    label: '选择题'
                }, {
                    value: '2',
                    label: '判断题'
                }, {
                    value: '3',
                    label: '多选题'
                }, {
                    value: '4',
                    label: '填空题'
                }, {
                    value: '5',
                    label: '简答题'
                }],

                typeRows: [{
                    value: '0',
                    label: '课后练习'
                }, {
                    value: '1',
                    label: '单元测试'
                }, {
                    value: '2',
                    label: '期中测试'
                }, {
                    value: '3',
                    label: '期末测试'
                }],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                }
            },
            methods: {
                onSubmit() {//新增试题
                    G.post('{JAVAURL}/sp-practice/addPractice', this.formInline).then((res) => {
                        console.log(res.data)

                        this.drawer = false;
                        const targetURL = 'http://localhost:3001/#/pages/practice/list';
                        window.location.href = targetURL;
                    });
                },
                getCharpterList() {//查询详情信息treeChapter?courseId=2
                    console.log("this.queryParms.courseId", this.queryParms.courseId);
                    this.treeData = [];
                    // 使用 this.$nextTick 强制更新视图
                    this.$nextTick(() => {
                        G.get('{JAVAURL}/sp-practice/treeChapter', { courseId: this.queryParms.courseId }).then((res) => {
                            console.log(res.data)
                            this.treeData = res.data;
                        });
                        /* G.post('{JAVAURL}/deducttype/list',this.params).then((res) => {
                           this.deductRows = res;
                         });*/
                    });

                },

                handleSelectionChange(val) {
                    this.formInline.multipleSelection = val;
                    console.log(this.formInline.multipleSelection);
                },
                getTopicList() {//查询详情信息treeChapter?courseId=2
                    console.log("this.queryParms.courseId", this.queryParms.courseId);
                    
                    G.get('{JAVAURL}/sp-practice/chapterTopic', this.queryParms).then((res) => {
                        console.log("res.data",res.data)
                        this.topicData = res.data;
                    });
                    /* G.post('{JAVAURL}/deducttype/list',this.params).then((res) => {
                       this.deductRows = res;
                     });*/
                },
                handleNodeClick(data) {
                    console.log(data.id);
                    this.queryParms.chapterId = data.id;
                    this.getTopicList();
                }
            },

            mounted(query) {
                console.log("query.SpCourseId", query.SpCourseId);
                this.queryParms.courseId = query.SpCourseId || '1313921323726012416';
                this.getCharpterList();
                this.getTopicList();


            }
        });
    })(Y)
</script>
<style>
    .el-dropdown-link {
        cursor: pointer;
        color: #409EFF;
    }

    .el-icon-arrow-down {
        font-size: 12px;
    }
</style>